package cn.dong.edu.controller.admin;


import cn.dong.edu.domain.dto.ChapterVo;
import cn.dong.edu.service.impl.ChapterServiceImpl;
import cn.dong.api.domain.CommonResult;
import cn.dong.edu.domain.Chapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程，章节 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/edu/chapter")
public class ChapterController {


    @Autowired
    private ChapterServiceImpl chapterService;

    /**
     * 根据课程id，查询全部的章节，包括小结
     * @return
     */
    @GetMapping("/findById/{courseId}")
    public CommonResult findById(@PathVariable("courseId") String courseId){
        List<ChapterVo> result = chapterService.findTotalById(courseId);
        return CommonResult.ok().data(result);
    }

    /**
     * 根据章节id，查询一个章节，不包括小结
     * @return
     */
    @GetMapping("/findOneById/{chapterId}")
    public CommonResult findOneById(@PathVariable("chapterId") String chapterId){
        Chapter result = chapterService.getById(chapterId);
        return CommonResult.ok().data(result);
    }

    /**
     * 修改章节
     * @param chapter
     * @return
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Chapter chapter){
        return CommonResult.whether(chapterService.updateById(chapter));
    }


    /**
     * 根据章节id，删除章节，并且删除全部的课时（小结）
     * @param chapterId
     * @return
     */
    @DeleteMapping("/delete/{chapterId}")
    public CommonResult delete(@PathVariable("chapterId") String chapterId){

        if (!chapterService.delete(chapterId)) {
            return CommonResult.error().message("删除失败！");
        }
        return CommonResult.ok();
    }


    /**
     * 创建章节
     * @param chapter
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return CommonResult.ok();
    }

}

